package com.jubaozan.service.platform.controller;

import com.jubaozan.c3.framework.constants.Constants;
import com.jubaozan.c3.framework.tools.ResultBuilder;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.dao.columns.Columns;
import com.jubaozan.feign.login.RoleTypeEnum;
import com.jubaozan.feign.login.vo.LoginVO;
import com.jubaozan.feign.login.vo.TokenVO;
import com.jubaozan.service.platform.modules.store.LoginDecorator;
import com.jubaozan.service.platform.vo.UserRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author yuanjie
 */
@RestController
@Slf4j
@Api(tags = "登录")
public class LoginController {
    private final LoginDecorator loginDecorator;

    public LoginController(LoginDecorator loginDecorator) {
        this.loginDecorator = loginDecorator;
    }

    @ApiOperation("登录(消费者/个体平台)")
    @PostMapping("/login")
    public ResultHolder<TokenVO> login(@RequestHeader(Constants.KEY_X_C3_STOREID) Long storeId,
                                       @RequestParam(Columns.TYPE) RoleTypeEnum type,
                                       @RequestBody LoginVO login) {
        if (Objects.isNull(login.getType())) {
            login.setType(type);
        }
        return ResultBuilder.success(loginDecorator.login(storeId, login))
                .withDescription("用户登录")
                .build();
    }

    @ApiOperation("查询用户所有身份信息")
    @GetMapping("/roles")
    public ResultHolder<List<UserRoleVO>> queryRoles(@RequestHeader(Constants.KEY_X_C3_AGENTID) Long userId) {
        return ResultBuilder.success(loginDecorator.queryRoles(userId))
                .withDescription("查询用户所有身份信息")
                .build();
    }
}
